/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 * aion-unique is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-unique is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package system.handlers.quest.ishalgen;

import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
import com.aionemu.gameserver.network.aion.serverpackets.SM_ITEM_USAGE_ANIMATION;
import com.aionemu.gameserver.network.aion.serverpackets.SM_PLAY_MOVIE;
import com.aionemu.gameserver.questEngine.QuestEngine;
import com.aionemu.gameserver.questEngine.handlers.QuestHandler;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.questEngine.model.QuestState;
import com.aionemu.gameserver.questEngine.model.QuestStatus;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.utils.ThreadPoolManager;

/**
 * @author Lyahim, Rhys2002, modified by Hellboy
 * 
 */
public class _2136TheLostAxe extends QuestHandler
{
	public static void main(String[] args)
	{
		QuestEngine.getInstance().addQuestHandler(new _2136TheLostAxe());
	}

	private final static int	questId	= 2136;
	private final static int[]	npc_ids	=
										{ 700146, 790009 };

	public _2136TheLostAxe()
	{
		super(questId);
		qe.setQuestItemIds(182203130).add(questId);
		for (int npc_id : npc_ids)
			qe.setNpcQuestData(npc_id).addOnTalkEvent(questId);
	}

	@Override
	public boolean onDialogEvent(QuestEnv env)
	{
		final Player player = env.getPlayer();
		final QuestState qs = player.getQuestStateList().getQuestState(questId);

		if (env.getVisibleObject() == null && (qs == null || qs.getStatus() == QuestStatus.NONE))
		{
			if (env.getDialogId() == 1002)
			{
				QuestEngine.startQuest(env, QuestStatus.START);
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(0, 0));
				return true;
			}
			else
				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(0, 0));
		}

		if (qs == null)
			return false;

		int var = qs.getQuestVarById(0);
		int targetId = env.getVisibleObject().getNpcId();
		if (qs.getStatus() == QuestStatus.REWARD)
		{
			if (targetId == 790009)
			{
				final Npc npc = (Npc) env.getVisibleObject();
				ThreadPoolManager.getInstance().schedule(new Runnable()
				{
					@Override
					public void run()
					{
						npc.onDelete();
					}
				}, 10000);
				return defaultQuestEndDialog(env);
			}
		}
		else if (qs.getStatus() != QuestStatus.START)
			return false;

		if (targetId == 790009)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 1)
						return sendQuestDialog(env, 1011);
				case 10000:
					if (var == 1)
					{
						qs.setStatus(QuestStatus.REWARD);
						updateQuestStatus(player, qs);
						Item.removeItemFromInventoryByItemId(player, 182203130);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 0));
						return sendQuestDialog(env, 6);
					}
				case 10001:
					if (var == 1)
					{
						qs.setStatus(QuestStatus.REWARD);
						updateQuestStatus(player, qs);
						Item.removeItemFromInventoryByItemId(player, 182203130);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 0));
						return sendQuestDialog(env, 5);
					}
			}
		}
		else if (targetId == 700146)
		{
			switch (env.getDialogId())
			{
				case -1:
					if (var == 0)
					{
						new NpcUseAction(player, env.getVisibleObject())
						{
							@Override
							protected void end()
							{
								super.end();
								respawn();
								PacketSendUtility.sendPacket(player, new SM_PLAY_MOVIE(0, 59));
								qs.setQuestVarById(0, 1);
								updateQuestStatus(player, qs);
								QuestEngine.addNewSpawn(220010000, player.getInstanceId(), 790009, 1088.5f, 2371.8f, 258.375f, (byte) 87, true);
							}
						};
					}
					return true;
			}
		}
		return false;
	}

	@Override
	public boolean onItemUseEvent(QuestEnv env, Item item)
	{
		final Player player = env.getPlayer();
		final int id = item.getItemTemplate().getTemplateId();
		final int itemObjId = item.getObjectId();
		QuestState qs = player.getQuestStateList().getQuestState(questId);

		if (id != 182203130)
			return false;
		PacketSendUtility.broadcastPacket(player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 20, 1, 0), true);
		if (qs == null || qs.getStatus() == QuestStatus.NONE)
			sendQuestDialog(env, 4);
		return true;
	}
}